//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.liy.cloud.auth.client.rest;

import com.liy.cloud.auth.client.feign.AuthorizationFeignClient;
import com.das.framework.common.annotation.ActionPermission;
import com.das.framework.common.annotation.MethodPermission;
import com.das.framework.common.extvo.ResultVO;
import com.das.framework.common.utils.Tools;
import com.das.framework.common.web.controller.SupportController;
import com.das.framework.common.web.utils.AopTargetUtils;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.condition.PatternsRequestCondition;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

@Controller
@RequestMapping({"/permission"})
@ActionPermission(
        code = "SYS.PERMISSION",
        description = "系统权限管理"
)
public class SysPermissionRest extends SupportController {
    @Autowired
    private ApplicationContext applicationContext;
    @Autowired
    private AuthorizationFeignClient authorizationFeignClient;
    @Value("${dascloud.sysId}")
    private String sysId;
    @Value("${dascloud.sysName}")
    private String sysName;

    public SysPermissionRest() {
    }

    @ResponseBody
    @RequestMapping({"/sync/test"})
    public ResultVO test() {
        ResultVO resultVO = new ResultVO(true, "测试方法");

        try {
            Thread.sleep(200L);
        } catch (InterruptedException var4) {
            ;
        }

        Map map = new HashMap();

        for(int i = 0; i < 100; ++i) {
            map.put(i, "AAAAAAAAAAAAAAAAAABBBDDD");
        }

        resultVO.setData(map);
        return resultVO;
    }

    @ResponseBody
    @RequestMapping({"/sync/url"})
    @MethodPermission(
            codes = {"SYNC_URL"},
            description = "权限Url同步"
    )
    public ResultVO sync() {
        ResultVO resultVO = new ResultVO();
        RequestMappingHandlerMapping beans = (RequestMappingHandlerMapping)this.applicationContext.getBean(RequestMappingHandlerMapping.class);
        Map<RequestMappingInfo, HandlerMethod> map = beans.getHandlerMethods();
        if (map != null && map.size() > 0) {
            List<Map<String, Object>> list = this.scanRequestMapping(map);
            resultVO = this.authorizationFeignClient.permissionSync(list);
            return resultVO;
        } else {
            resultVO.setSuccess(false);
            resultVO.setMessage("获取容器信息失败!");
            return resultVO;
        }
    }

    @ResponseBody
    @RequestMapping({"/sync/code"})
    @MethodPermission(
            codes = {"SYNC_CODE"},
            description = "权限码同步"
    )
    public ResultVO sync2() {
        ResultVO resultVO = new ResultVO();
        Map<String, Object> map = this.applicationContext.getBeansWithAnnotation(ActionPermission.class);
        if (map != null && map.size() > 0) {
            List<Map<String, Object>> list = this.scanPermission(map);
            resultVO = this.authorizationFeignClient.permissionSync(list);
            return resultVO;
        } else {
            resultVO.setSuccess(false);
            resultVO.setMessage("获取容器信息失败!");
            return resultVO;
        }
    }

    public List<Map<String, Object>> scanRequestMapping(Map<RequestMappingInfo, HandlerMethod> map) {
        List<Map<String, Object>> permissionList = new ArrayList();
        HashMap permission = null;
        int index = 0;
        Iterator var5 = map.entrySet().iterator();

        while(true) {
            HandlerMethod handlerMethod;
            String urls;
            do {
                if (!var5.hasNext()) {
                    log.info("权限URL数据扫描结束，结果:" + permissionList.size());
                    return permissionList;
                }

                Entry<RequestMappingInfo, HandlerMethod> entry = (Entry)var5.next();
                RequestMappingInfo rmi = (RequestMappingInfo)entry.getKey();
                handlerMethod = (HandlerMethod)entry.getValue();
                PatternsRequestCondition prc = rmi.getPatternsCondition();
                urls = prc != null ? StringUtils.join(prc.getPatterns(), ",") : "无";
            } while(urls.length() <= 1);

            String shared = "1";
            String ccode = "N";
            String cdescription = "N";
            String mcode = "N";
            String mdescription = "N";
            ActionPermission action = (ActionPermission)handlerMethod.getBeanType().getAnnotation(ActionPermission.class);
            if (action == null) {
                try {
                    Class clazz = AopTargetUtils.getTarget(handlerMethod.getBeanType()).getClass();
                    action = (ActionPermission)clazz.getAnnotation(ActionPermission.class);
                } catch (Exception var18) {
                    ;
                }
            }

            if (action != null) {
                ccode = action.code().trim();
                cdescription = action.description().trim();
            }

            MethodPermission method = (MethodPermission)handlerMethod.getMethodAnnotation(MethodPermission.class);
            if (method != null) {
                shared = method.share();
                mcode = ccode + "." + (method.codes() != null && method.codes().length > 0 ? method.codes()[0] : "");
                mdescription = method.description().trim();
            }

            permission = new HashMap();
            permission.put("url", urls);
            permission.put("code", mcode);
            permission.put("name", mdescription);
            permission.put("modelName", cdescription);
            permission.put("sno", index++);
            permission.put("sysId", this.sysId);
            permission.put("sysName", this.sysName);
            permission.put("source", handlerMethod.getBean());
            permission.put("shared", shared);
            permission.put("status", "1");
            permission.put("type", "0");
            permissionList.add(permission);
        }
    }

    public List<Map<String, Object>> scanPermission(Map<String, Object> map) {
        Set<Entry<String, Object>> set = map.entrySet();
        List<Map<String, Object>> permissionList = new ArrayList();
        Map<String, Object> permission = null;
        int index = 0;
        Iterator var6 = set.iterator();

        while(true) {
            String key;
            String classUrl;
            Class clazz;
            ActionPermission action;
            do {
                do {
                    do {
                        if (!var6.hasNext()) {
                            log.info("权限数据扫描结束，结果:" + permissionList.size());
                            return permissionList;
                        }

                        Entry<String, Object> entry = (Entry)var6.next();
                        key = (String)entry.getKey();
                        Object val = entry.getValue();
                        classUrl = "";
                        clazz = val.getClass();
                        action = (ActionPermission)clazz.getAnnotation(ActionPermission.class);
                        if (action == null) {
                            try {
                                clazz = AopTargetUtils.getTarget(val).getClass();
                                action = (ActionPermission)clazz.getAnnotation(ActionPermission.class);
                            } catch (Exception var22) {
                                ;
                            }
                        }
                    } while(action == null);
                } while(Tools.StringIsNullOrSpace(action.code()));
            } while(Tools.StringIsNullOrSpace(action.description()));

            RequestMapping requestMapping = (RequestMapping)clazz.getAnnotation(RequestMapping.class);
            if (requestMapping != null && requestMapping.value() != null && requestMapping.value().length > 0) {
                classUrl = requestMapping.value()[0].trim();
            }

            Method[] methds = clazz.getMethods();
            Method[] var15 = methds;
            int var16 = methds.length;

            for(int var17 = 0; var17 < var16; ++var17) {
                Method method = var15[var17];
                String[] urls = this.getRequestUrl(method);
                if (urls != null && urls.length > 0) {
                    String url = StringUtils.join(urls, ",");
                    MethodPermission m = (MethodPermission)method.getAnnotation(MethodPermission.class);
                    if (m != null && !Tools.StringIsNullOrSpace(m.codes().toString()) && !Tools.StringIsNullOrSpace(m.description())) {
                        permission = new HashMap();
                        permission.put("url", classUrl + url);
                        permission.put("code", action.code().trim() + "." + m.codes()[0].toString().trim());
                        permission.put("name", m.description().trim());
                        permission.put("mName", action.description().trim());
                        permission.put("sno", index++);
                        permission.put("sysId", this.sysId);
                        permission.put("sysName", this.sysName);
                        permission.put("source", key);
                        permissionList.add(permission);
                    }
                }
            }
        }
    }

    private String[] getRequestUrl(Method method) {
        RequestMapping requestMapping = (RequestMapping)method.getAnnotation(RequestMapping.class);
        if (requestMapping != null) {
            return requestMapping.value();
        } else {
            GetMapping getMapping = (GetMapping)method.getAnnotation(GetMapping.class);
            if (getMapping != null) {
                return getMapping.value();
            } else {
                PostMapping postMapping = (PostMapping)method.getAnnotation(PostMapping.class);
                if (postMapping != null) {
                    return postMapping.value();
                } else {
                    PutMapping putMapping = (PutMapping)method.getAnnotation(PutMapping.class);
                    if (putMapping != null) {
                        return putMapping.value();
                    } else {
                        DeleteMapping deleteMapping = (DeleteMapping)method.getAnnotation(DeleteMapping.class);
                        return deleteMapping != null ? deleteMapping.value() : null;
                    }
                }
            }
        }
    }
}
